<?php
/**
 * Created by PhpStorm.
 * User: fujianguo
 * Date: 2017/3/20
 * Time: 下午4:33
 */

namespace common\models;


use yii\base\Exception;
use yii\base\Object;

class ArchiveFactory extends Object
{
    /**
     * @param $arcType
     * @return Archive
     * @throws Exception
     */
    public static function factory($arcType)
    {
        $config = include 'configs/archives.php';
        if (!array_key_exists($arcType,$config)) throw new Exception('没有找到该类型的实例：'.$arcType);
        return \Yii::createObject($config[$arcType]);
    }

    /**
     * 通过类名找到定义好的arc_type
     */
    public static function getTypeByClassName($className)
    {
        $arcType = null;
        if (is_object($className)) {
            $className = get_class($className);
        }
        $configs = include 'configs/archives.php';
        foreach ($configs as $key => $row) {
            if (trim($row['class'],'\\') == trim($className,'\\')) {
                $arcType = $row['arc_type'];
                break;
            }
        }
        return $arcType;
    }
}